package net.yxsoft.service.infrastructure;

import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import net.dreamlu.event.EventKit;
import net.yxsoft.enumclass.SexEnumCls;
import net.yxsoft.enumclass.TsUserTypeEnumCls;
import net.yxsoft.enumclass.WhetherEnumCls;
import net.yxsoft.kit.*;

import net.yxsoft.resource.PathRes;
import net.yxsoft.resource.Res;
import rapid.core.Authuser;

import java.util.*;

/**
 * 系统初始化
 *
 * @author zhaohaibo
 */
public class SystemService {

  private SystemService() {
  }

  /**
  * @MethodName: start
  * @Description: 默认启动函数
  * @param:
  * @return * @return void
  * @throws
  * @date 2023/8/31 13:30
  * @author wj
  */
  public static void start() {
    initMgtMenus();
    initGroupBaseData();
    initRoleBaseData();
    initUserBaseData();



  }


  /**
  * @MethodName: initMgtMenus
  * @Description: 第一步：初始化系统管理菜单
  * @param:
  * @return * @return void
  * @throws
  * @date 2023/8/31 13:31
  * @author wj
  */
  public static void initMgtMenus() {
    Record rd = Db.findFirst("select count(1) num from ts_menu");

    if(rd.getInt("num") > 0) {
      return;
    }

    List<String> sqls = new ArrayList<String>();

    sqls.add("INSERT INTO ts_menu VALUES ('1', '0', '1', '系统管理', '" + Res.PC_MGT_MENU_CODE_SYS + "', null, null, '1', '1', null, '99','ORDINARYMENU',null)");
    sqls.add("INSERT INTO ts_menu VALUES ('2', '1', '2', '机构管理', '9903', null, '" + PathRes.URL_PC_MGT_ROOT + "/group', '1', '1', null, '1','ORDINARYMENU',null)");
    sqls.add("INSERT INTO ts_menu VALUES ('3', '1', '2', '角色管理', '9902', null, '" + PathRes.URL_PC_MGT_ROOT + "/role', '1', '1', null, '2','ORDINARYMENU',null)");
    sqls.add("INSERT INTO ts_menu VALUES ('4', '1', '2', '用户管理', '9901', null, '" + PathRes.URL_PC_MGT_ROOT + "/user', '1', '1', null, '3','ORDINARYMENU',null)");
    sqls.add("INSERT INTO ts_menu VALUES ('5', '1', '2', '数据字典', '9905', null, '" + PathRes.URL_PC_MGT_ROOT + "/code', '1', '1', null, '4','ORDINARYMENU',null)");
    sqls.add("INSERT INTO ts_menu VALUES ('6', '1', '2', '日志查询', '9906', null, '" + PathRes.URL_PC_MGT_ROOT + "/log', '1', '1', null, '5','ORDINARYMENU',null)");

    Db.batch(sqls, 10000);
  }

  /**
  * @MethodName: initGroupBaseData
  * @Description: 第二步：初始化ts_group表基础数据
  * @param:
  * @return * @return void
  * @throws
  * @date 2023/8/31 13:31
  * @author wj
  */
  public static void initGroupBaseData() {
    final String SQL = "select * from ts_group where code = ?";

    Record rd = Db.findFirst(SQL, Res.GROUP_ROOT_CODE);


    if(rd == null) {
      CustomDb.updateWithExceptionHandling("insert into ts_group (id,code, pid, name, enabled, locked) values (?,?, ? ,?, ?, ?)",
              Res.GROUP_ROOT_CODE,Res.GROUP_ROOT_CODE, Res.GROUP_ROOT_FIRST_CODE, Res.GROUP_ROOT_NAME, "1", "0");

      rd = Db.findFirst(SQL, Res.GROUP_ROOT_CODE);

      Res.groupRootId = rd.getStr("id");

    } else {
      Res.groupRootId = rd.getStr("id");
    }
  }

  /**
  * @MethodName: initRoleBaseData
  * @Description: 第三步：初始化ts_role表基础数据
  * @param:
  * @return * @return void
  * @throws
  * @date 2023/8/31 13:31
  * @author wj
  */
  public static void initRoleBaseData() {
    final String ROLE_CODE = Res.ADMIN_ROLE_CODE;
    final String ROLE_NAME = Res.ADMIN_ROLE_NAME;
    final String YES = WhetherEnumCls.YES.getValue();
    final String NO = WhetherEnumCls.NO.getValue();

    final String SQL = "select * from ts_role where name = ? and code = ?";
    Record rd = Db.findFirst(SQL, ROLE_NAME, ROLE_CODE);

    if(rd == null) {
      CustomDb.updateWithExceptionHandling("insert into ts_role (code, name, enabled, locked, sort_id) values (?, ?, ?, ?, ?)",
              ROLE_CODE, ROLE_NAME, YES, NO, "0");

      rd = Db.findFirst(SQL, ROLE_NAME, ROLE_CODE);

      Res.adminRoleId = rd.getStr("id");

    } else {
      Res.adminRoleId = rd.getStr("id");
    }
  }

  /**
  * @MethodName: initUserBaseData
  * @Description:  第四步：初始化ts_user表基础数据以及关联数据
  * @param:
  * @return * @return void
  * @throws
  * @date 2023/8/31 13:32
  * @author wj
  */
  public static void initUserBaseData() {
    final String USER_CODE = Res.ADMIN_USER_LOGIN_ID;
    final String YES = WhetherEnumCls.YES.getValue();
    final String SQL = "select * from ts_user where login_id = ? and code = ?";

    Record rd = Db.findFirst(SQL, USER_CODE, USER_CODE);

    if(rd == null) {
      CustomDb.updateWithExceptionHandling("insert into ts_user (login_id, code, password, name, note, status, sex, user_typ, enabled, canlogin, sort_id) values " +
                      "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
              USER_CODE, USER_CODE, HashKit.sha256(USER_CODE + Res.USER_DEFAULT_PASSWORD), Res.ADMIN_USER_NAME,
              "注意：删除该用户将影响系统操作，请勿删除。", YES, SexEnumCls.Man.getValue(), TsUserTypeEnumCls.TYPE_0.getValue(),
              YES, YES, "0");

      rd = Db.findFirst(SQL, USER_CODE, USER_CODE);

      Res.adminUserId = rd.getStr("id");

    } else {
      Res.adminUserId = rd.getStr("id");
    }

    rd = Db.findFirst("select * from ts_ug_relation where user_id = ? and group_id = ?",
            Res.adminUserId, Res.groupRootId);

    if(rd == null) {
      CustomDb.updateWithExceptionHandling("insert into ts_ug_relation values (?, ?)", Res.adminUserId, Res.groupRootId);
    }

    rd = Db.findFirst("select * from ts_ur_relation where user_id = ? and role_id = ?",
            Res.adminUserId, Res.adminRoleId);


    if(rd == null) {
      CustomDb.updateWithExceptionHandling("insert into ts_ur_relation values (?, ?)", Res.adminUserId, Res.adminRoleId);
    }
  }
  








}
